/**
 * @author 
 * @createtime 
 * @class ProjectForm
 * @extends Ext.Window
 * @description Project表单
 */
ProjectForm=Ext.extend(Ext.Window,{
	//内嵌FormPanel
	formPanel:null,
	//构造函数
	constructor:function(_cfg){
		Ext.applyIf(this,_cfg);
		//必须先初始化组件
		this.initUIComponents();
		ProjectForm.superclass.constructor.call(this,{
			id:'ProjectFormWin',
			layout:'fit',
			items:this.formPanel,
			modal:true,
			height:600,
			width:600,
			maximizable:true,
			title:'[项目登记]详细信息',
			buttonAlign : 'center',
			buttons:this.buttons
		});
	},//end of the constructor
	//初始化组件
	initUIComponents:function(){
		this.formPanel=new Ext.FormPanel({
				layout : 'form',
				trackResetOnLoad:true,
				autoScroll : true,
			    bodyStyle: 'padding:10px 10px 10px 10px;overflow-x:hidden;overflow-y:scroll',
				border:false,
				url : __ctxPath + '/project/saveProject.do',
				id : 'ProjectForm',
				defaults : {
					anchor : '98%,98%'
				},
				defaultType : 'textfield',
				items : [{
										name : 'project.id',
										id : 'id',
										xtype : 'hidden',
										value : this.id == null ? '' : this.id
									}, {
										fieldLabel : '项目编号',
										name : 'project.proNo',
										id : 'proNo',
										readOnly:true,
										emptyText:'系统自动生成！'
									}, {
										fieldLabel : '工程名称',
										allowBlank:false,
										name : 'project.proName',
										id : 'proName',
										emptyText:'请选择项目中标确认书',
										listeners : {
											focus : function() {
												ProjectWinbidSelector.getView(function(winbidId, proName, winBidAmount,contractWorkLimit,managerUserId,managerUserName,contractWorkLimitEnd) {
													Ext.getCmp("proName").setValue(proName);
													Ext.getCmp("contractAmount").setValue(winBidAmount);
													Ext.getCmp("contractAmount").fireEvent('keyup',Ext.getCmp("contractAmount"));
													Ext.getCmp("contractWorkDate").setValue(contractWorkLimit);
													Ext.getCmp("contractWorkEndDate").setValue(contractWorkLimitEnd);
													Ext.getCmp("managerName").setValue(managerUserName);
													Ext.getCmp("project.manager.userId").setValue(managerUserId);
												}, true).show();
											}
										}
									}, {
										fieldLabel : '项目经理',
										name : 'managerName',
										id : 'managerName',
										emptyText:'请选择项目经理',
										allowBlank:false,
										listeners:{
											focus:function(){
												UserSelector.getView(function(ids, names) {
														Ext.getCmp("managerName").setValue(names);
														Ext.getCmp("project.manager.userId").setValue(ids);
													}, true).show();
											}
										}
									},{
										xtype:'hidden',
										name:'project.manager.userId',
									 	id:'project.manager.userId'
									}, {
										fieldLabel : '跟踪决算员',
										name : 'followerName',
										id : 'followerName',
										emptyText:'请选择跟踪决算员',
										allowBlank:false,
										listeners:{
											focus:function(){
												UserSelector.getView(function(ids, names) {
														Ext.getCmp("followerName").setValue(names);
														Ext.getCmp("project.follower.userId").setValue(ids);
													}, true).show();
											}
										}
									},{
										xtype:'hidden',
										name:'project.follower.userId',
									 	id:'project.follower.userId'
									}, {
										fieldLabel : '合同金额',
										allowBlank:false,
										name : 'project.contractAmount',
										id : 'contractAmount',
										enableKeyEvents:true,
										xtype:'numberfield',
										regex:/^(([1-9]\d{0,9})|0)(\.\d{1,2})?$/,
										regexText:'请输入正确格式的金额',
										listeners : {
											keyup: function(field) {
												var contractAmountBig = AmountInWords(field.getValue());
												Ext.getCmp('contractAmountBig').setValue(contractAmountBig); 
											}
										}
									}, {
										fieldLabel : '合同金额大写',
										name : 'project.contractAmountBig',
										id : 'contractAmountBig',
										readOnly:true
									}, {
										fieldLabel : '增减金额',
										name : 'project.addOrMinus',
										id : 'addOrMinus',
										xtype:'combo',
										triggerAction:'all',
										mode:'local',
										store:['+','-']
									}, {
										fieldLabel : '合同工期开始日期',
										xtype:'datefield',
										format:'Y-m-d',
										name : 'project.contractWorkDate',
										id : 'contractWorkDate'
									}, {
										fieldLabel : '合同工期结束日期',
										xtype:'datefield',
										format:'Y-m-d',
										name : 'project.contractWorkEndDate',
										id : 'contractWorkEndDate'
									}, {
										fieldLabel : '进场日期',
										name : 'project.enterDate',
										id : 'enterDate',
										xtype:'datefield',
										format:'Y-m-d'
									}, {
										fieldLabel : '质量要求',
										name : 'project.quality',
										id : 'quality'
									}, {
										fieldLabel : '完工前付款%',
										name : 'project.payBeforeEnd',
										id : 'payBeforeEnd',
										xtype:'numberfield',
										maxLength:5,
										regex:/^(([1-9]\d{0,2})|0)(\.\d{1,2})?$/
									}, {
										fieldLabel : '结算方式',
										allowBlank : false,
										xtype:'combo',
										hiddenName : 'project.balanceType',
										id : 'balanceType',
										selectOnFocus : true,
										forceSelection : true,
										editable:false,
										triggerAction : 'all',
										mode : 'remote',
										valueField : "itemValue",
										displayField : "itemValue",
										store : new Ext.data.JsonStore({
											baseParams : {
												"Q_itemName_S_EQ" : '结算方式'
											},
											autoLoad : true,
											root : "result",
											totalProperty : "totalCounts",
											url : __ctxPath + "/system/listDictionary.do",
											fields : ["dicId", "itemName", "itemValue"]
										})
									}, {
										fieldLabel : '项目说明',
										name : 'project.proDesc',
										id : 'proDesc',
										xtype:'textarea'
									}, {
										fieldLabel : '仓管员',
										name : 'keeperName',
										id : 'keeperName',
										emptyText:'请选择仓管员',
										allowBlank:false,
										listeners:{
											focus:function(){
												UserSelector.getView(function(ids, names) {
														Ext.getCmp("keeperName").setValue(names);
														Ext.getCmp("project.keeper.userId").setValue(ids);
													}, true).show();
											}
										}
									},{
										xtype:'hidden',
										name:'project.keeper.userId',
									 	id:'project.keeper.userId'
									}, {
										fieldLabel : '园建施工员',
										name : 'gardenUserNames',
										id : 'gardenUserNames',
										emptyText:'请选择园建施工员',
										xtype : 'textarea',
										listeners : {
											focus : function() {
												UserSelector.getView(function(ids, names) {
													Ext.getCmp("gardenUserNames").setValue(names);
													Ext.getCmp("gardenUserIds").setValue(ids);
												}, false).show();
											}
										}
									}, {
										xtype : 'hidden',
										name : 'gardenUserIds',
										id : 'gardenUserIds'
									}, {
										fieldLabel : '绿化施工员',
										name : 'greenUserNames',
										id : 'greenUserNames',
										emptyText:'请选择绿化施工员',
										xtype : 'textarea',
										listeners : {
											focus : function() {
												UserSelector.getView(function(ids, names) {
													Ext.getCmp("greenUserNames").setValue(names);
													Ext.getCmp("greenUserIds").setValue(ids);
												}, false).show();
											}
										}
									}, {
										xtype : 'hidden',
										name : 'greenUserIds',
										id : 'greenUserIds'
									}, {
										fieldLabel : '水电施工员',
										name : 'waterUserNames',
										id : 'waterUserNames',
										emptyText:'请选择水电施工员',
										xtype : 'textarea',
										listeners : {
											focus : function() {
												UserSelector.getView(function(ids, names) {
													Ext.getCmp("waterUserNames").setValue(names);
													Ext.getCmp("waterUserIds").setValue(ids);
												}, false).show();
											}
										}
									}, {
										xtype : 'hidden',
										name : 'waterUserIds',
										id : 'waterUserIds'
									}, {
										fieldLabel : '资料员',
										name : 'materUserNames',
										id : 'materUserNames',
										emptyText:'请选择资料员',
										xtype : 'textarea',
										listeners : {
											focus : function() {
												UserSelector.getView(function(ids, names) {
													Ext.getCmp("materUserNames").setValue(names);
													Ext.getCmp("materUserIds").setValue(ids);
												}, false).show();
											}
										}
									}, {
										xtype : 'hidden',
										name : 'materUserIds',
										id : 'materUserIds'
									}, {
										fieldLabel : '决算员',
										name : 'finalUserNames',
										id : 'finalUserNames',
										emptyText:'请选择决算员',
										xtype : 'textarea',
										listeners : {
											focus : function() {
												UserSelector.getView(function(ids, names) {
													Ext.getCmp("finalUserNames").setValue(names);
													Ext.getCmp("finalUserIds").setValue(ids);
												}, false).show();
											}
										}
									}, {
										xtype : 'hidden',
										name : 'finalUserIds',
										id : 'finalUserIds'
									}, {
										fieldLabel : '质量员',
										name : 'qualityUserNames',
										id : 'qualityUserNames',
										emptyText:'请选择质量员',
										xtype : 'textarea',
										listeners : {
											focus : function() {
												UserSelector.getView(function(ids, names) {
													Ext.getCmp("qualityUserNames").setValue(names);
													Ext.getCmp("qualityUserIds").setValue(ids);
												}, false).show();
											}
										}
									}, {
										xtype : 'hidden',
										name : 'qualityUserIds',
										id : 'qualityUserIds'
									}, {
										fieldLabel : '备注',
										name : 'project.remark',
										id : 'remark',
										xtype:'textarea'
									},{
										xtype : 'container',
										autoHeight : true,
										layout : 'column',
										autoWidth : true,
										defaultType : 'label',
										style : 'padding-left:0px;padding-bottom:5px;',
										items : [{
													text : '附件:',
													width:95,
													style : "margin-top:15px;"
												},{
													xtype : 'hidden',
													name : 'attachIds',
													id : 'project.attachIds'
												},{
													xtype : 'hidden',
													name : 'attachFiles',
													id : 'project.attachFiles'
												},{
													xtype : 'panel',
													id:'project.displayAttach',
													columnWidth:.95,
													height: 65,
													frame:false,
													autoScroll:true,
													style : 'padding-left:10px;padding-top:0px;',
													html : ''
												},{
													xtype : 'button',
													iconCls : 'btn-upload',
													text : '上传',
													handler : function() {
														var dialog = App.createUploadDialog({
																				file_cat : 'flow/project',
																				callback : function(data) {
																					Ext.getCmp('project.displayAttach').body.update('');
																					Ext.getCmp("project.attachFiles").setValue('');
																					Ext.getCmp('project.attachIds').setValue('');
																					var attachFiles = Ext.getCmp("project.attachFiles");
																					var fileIds = Ext.getCmp('project.attachIds');
																					var display = Ext.getCmp('project.displayAttach');
																					for (var i = 0; i < data.length; i++) {
																						if (fileIds.getValue() != '') {
																							fileIds.setValue(fileIds.getValue()+ ',');
																							attachFiles.setValue(attachFiles.getValue()+ ',');
																						}
																						attachFiles.setValue(attachFiles.getValue()+data[i].filepath+":"+data[i].filename.replace(/\s+/g,""));
																						fileIds.setValue(fileIds.getValue()+data[i].fileId);
																						Ext.DomHelper.append(display.body,'<span><a href="#" onclick="FileAttachDetail.show('+data[i].fileId+')">'+data[i].filename.replace(/\s+/g,"")+'</a>&nbsp;|&nbsp;</span>');
																					}
																				},
																				permitted_max_size :1024*1024*20
																			});
															dialog.show(this);
													}
												}]
									}

							]
			});
		// 加载表单对应的数据
		this.initData();
		// 初始化功能按钮
		this.buttons=[{
				text : '保存',
				iconCls : 'btn-save',
				handler :this.save.createCallback(this.formPanel,this)
			}, {
				text : '重置',
				iconCls : 'btn-reset',
				handler :this.reset.createCallback(this.formPanel)
			},{
				text : '取消',
				iconCls : 'btn-cancel',
				handler : this.cancel.createCallback(this)
			}];
	},// end of the initcomponents
	/**
	 * 初始化数据
	 * 
	 * @param {}
	 *            formPanel
	 */
	initData:function(){
		//加载表单对应的数据	
		if (this.id != null && this.id != 'undefined') {
			this.formPanel.getForm().load({
				deferredRender : false,
				url : __ctxPath + '/project/getProject.do?id='+ this.id,
				waitMsg : '正在载入数据...',
				success : function(form, action) {
						var jsonData = Ext.util.JSON.decode(action.response.responseText);
						var res = jsonData.data;
						if(!Ext.isEmpty(res.manager)){
							Ext.getCmp("project.manager.userId").setValue(res.manager.userId);
							Ext.getCmp("project.manager.userId").originalValue=res.manager.userId;
							Ext.getCmp("managerName").setValue(res.manager.fullname);
							Ext.getCmp("managerName").originalValue=res.manager.fullname;
						}
						if(!Ext.isEmpty(res.follower)){
							Ext.getCmp("project.follower.userId").setValue(res.follower.userId);
							Ext.getCmp("project.follower.userId").originalValue=res.follower.userId;
							Ext.getCmp("followerName").setValue(res.follower.fullname);
							Ext.getCmp("followerName").originalValue=res.follower.fullname;
						}
						if(!Ext.isEmpty(res.keeper)){
							Ext.getCmp("project.keeper.userId").setValue(res.keeper.userId);
							Ext.getCmp("project.keeper.userId").originalValue=res.keeper.userId;
							Ext.getCmp("keeperName").setValue(res.keeper.fullname);
							Ext.getCmp("keeperName").originalValue=res.keeper.fullname;
						}
						if(!Ext.isEmpty(res.projectUsers)){
							var gardenUserNames = "";
							var gardenUserIds = "";
							var greenUserNames = "";
							var greenUserIds = "";
							var waterUserNames = "";
							var waterUserIds = "";
							var materUserNames = "";
							var materUserIds = "";
							var finalUserNames = "";
							var finalUserIds = "";
							var qualityUserNames = "";
							var qualityUserIds = "";
							Ext.each(res.projectUsers,function(item,i){
								switch(item.type){
									case 1:
										if(gardenUserNames!=""){
											gardenUserNames+=","
											gardenUserIds+=","
										}
										gardenUserNames+=item.user.fullname;
										gardenUserIds+=item.user.userId;
										break;
									case 2:
										if(greenUserNames!=""){
											greenUserNames+=","
											greenUserIds+=","
										}
										greenUserNames+=item.user.fullname;
										greenUserIds+=item.user.userId;
										break;
									case 3:
										if(waterUserNames!=""){
											waterUserNames+=","
											waterUserIds+=","
										}
										waterUserNames+=item.user.fullname;
										waterUserIds+=item.user.userId;
										break;
									case 4:
										if(materUserNames!=""){
											materUserNames+=","
											materUserIds+=","
										}
										materUserNames+=item.user.fullname;
										materUserIds+=item.user.userId;
										break;
									case 5:
										if(finalUserNames!=""){
											finalUserNames+=","
											finalUserIds+=","
										}
										finalUserNames+=item.user.fullname;
										finalUserIds+=item.user.userId;
										break;
									case 6:
										if(qualityUserNames!=""){
											qualityUserNames+=","
											qualityUserIds+=","
										}
										qualityUserNames+=item.user.fullname;
										qualityUserIds+=item.user.userId;
										break;
								}
							});
							Ext.getCmp("gardenUserNames").setValue(gardenUserNames);
							Ext.getCmp("gardenUserNames").originalValue = gardenUserNames;
							Ext.getCmp("gardenUserIds").setValue(gardenUserIds);
							Ext.getCmp("gardenUserIds").originalValue = gardenUserIds;
							
							Ext.getCmp("greenUserNames").setValue(greenUserNames);
							Ext.getCmp("greenUserNames").originalValue = greenUserNames;
							Ext.getCmp("greenUserIds").setValue(greenUserIds);
							Ext.getCmp("greenUserIds").originalValue = greenUserIds;
							
							Ext.getCmp("waterUserNames").setValue(waterUserNames);
							Ext.getCmp("waterUserNames").originalValue = waterUserNames;
							Ext.getCmp("waterUserIds").setValue(waterUserIds);
							Ext.getCmp("waterUserIds").originalValue = waterUserIds;
							
							Ext.getCmp("materUserNames").setValue(materUserNames);
							Ext.getCmp("materUserNames").originalValue = materUserNames;
							Ext.getCmp("materUserIds").setValue(materUserIds);
							Ext.getCmp("materUserIds").originalValue = materUserIds;
							
							Ext.getCmp("finalUserNames").setValue(finalUserNames);
							Ext.getCmp("finalUserNames").originalValue = finalUserNames;
							Ext.getCmp("finalUserIds").setValue(finalUserIds);
							Ext.getCmp("finalUserIds").originalValue = finalUserIds;
							
							Ext.getCmp("qualityUserNames").setValue(qualityUserNames);
							Ext.getCmp("qualityUserNames").originalValue = qualityUserNames;
							Ext.getCmp("qualityUserIds").setValue(qualityUserIds);
							Ext.getCmp("qualityUserIds").originalValue = qualityUserIds;
						}
						var af = res.fileAttachs;
						var filePanel = Ext.getCmp('project.displayAttach');
						var fileIds = Ext.getCmp("project.attachIds");
						for (var i = 0; i < af.length; i++) {
							if (fileIds.getValue() != '') {
								fileIds.setValue(fileIds.getValue() + ',');
							}
							fileIds.setValue(fileIds.getValue() + af[i].fileId);
							Ext.DomHelper
									.append(
											filePanel.body,
											'<span><a href="#" onclick="FileAttachDetail.show('
													+ af[i].fileId
													+ ')">'
													+ af[i].fileName.replace(/\s+/g,"")+'</a>&nbsp;|&nbsp;</span>');
						}
						fileIds.originalValue = fileIds.getValue();
				},
				failure : function(form, action) {
				}
			});
		}
	},
	submitEmptyText: function(){//解决 emptyText传值到后台
		var form = this.formPanel.getForm();
		var submitValues = form.getValues();
		for (var param in submitValues) {
			if (form.findField(param).emptyText == submitValues[param]) {
				form.findField(param).emptyText = '';
				form.findField(param).setValue('');
			}
		}
	},
	/**
	 * 重置
	 * @param {} formPanel
	 */
	reset:function(formPanel){
		formPanel.getForm().reset();
	},
	/**
	 * 取消
	 * @param {} window
	 */
	cancel:function(window){
		window.close();
	},
	/**
	 * 保存记录
	 */
	save:function(formPanel,window){
		if (formPanel.getForm().isValid()) {
			formPanel.getForm().submit({
				method : 'POST',
				waitMsg : '正在提交数据...',
				success : function(fp, action) {
					Ext.ux.Toast.msg('操作信息', '成功保存信息！');
					var gridPanel=Ext.getCmp('ProjectGrid');
					if(gridPanel!=null){
						gridPanel.getStore().reload();
					}
					window.close();
				},
				failure : function(fp, action) {
					Ext.MessageBox.show({
								title : '操作信息',
								msg : '信息保存出错，请联系管理员！',
								buttons : Ext.MessageBox.OK,
								icon : Ext.MessageBox.ERROR
							});
					window.close();
				}
			});
		}
	}//end of save
	
});